package com.suredy.flow;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import org.apache.commons.lang3.StringUtils;

import com.suredy.core.helper.ApplicationContextHelper;
import com.suredy.fw.model.FWInfo;
import com.suredy.fw.service.FWInfoSrv;

import eabnp.eflow.api.TodoItemInfo;
import eabnp.eflow.info.ctrlinfo.processctrl.actresult.ActResultListCtrl;

public class FlowRmiSrvImpl extends UnicastRemoteObject
  implements IFlowRmiSrv
{
  private static final long serialVersionUID = 1L;
  private String name;
  

  public FlowRmiSrvImpl()
    throws RemoteException
  {
  }

  public TodoItemInfo SetTodeInfo(TodoItemInfo info, String appType, String fileId)
    throws RemoteException
  {
	if(appType.equals("fw")){
		FWInfoSrv fwsvr = ApplicationContextHelper.getBeanByType(FWInfoSrv.class);
		FWInfo fwInfo = fwsvr.get(fileId);
		info.setTitle(fwInfo.getTitle());
		info.setLink(createTodoUri("/test/appfw/fwform/{id}", fwInfo.getId()));
	}
    
    return info;
  }

  public boolean actRsProcess(ActResultListCtrl actRsCtrl)
    throws RemoteException
  {
    return false;
  }

  public FlowRmiSrvImpl(String name) throws RemoteException
  {
    this.name = name;
  }
  
  protected String createTodoUri(String formUri, String fileId) {
		if (StringUtils.isBlank(formUri) || StringUtils.isBlank(fileId))
			throw new IllegalArgumentException("Invalid parameters. Please check it now.");

		if (formUri.indexOf("{id}") == -1)
			throw new IllegalArgumentException("Invalid parameter String[formUri]. It must contain a placeholder '{id}'.");

		String cp = InitFlowRmi.context_path;

		if (!StringUtils.isBlank(cp) && formUri.startsWith(cp))
			cp = "";

		if (!formUri.startsWith("/"))
			cp += "/";

		formUri = cp + formUri;

		return formUri.replace("{id}", fileId);
	}
}